var req;
var globalPage;
var globalIsInbox;

function getMailList(page, isInbox)
{
    var url = "/SRS-war/MailPagingServlet?page=" + page + "&isInbox=1";
    if(isInbox == 0)
    {
        url = "/SRS-war/MailPagingServlet?page=" + page + "&isInbox=0";
    }
    globalIsInbox = isInbox;
//    alert(url);

    globalPage = page;

    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest( );
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    req.open("Get", url, true);
    req.onreadystatechange = callback;
    req.send(null);
}

function nonMSPopulate( )
{
    xmlDoc = document.implementation.createDocument("","", null);
    var resp = req.responseText;
    var parser = new DOMParser( );
    var dom = parser.parseFromString(resp,"text/xml");

    mailIDVal = dom.getElementsByTagName("mailID");

    fromUserVal = dom.getElementsByTagName("fromUser");

    subjectVal = dom.getElementsByTagName("subject");

    display();
}

function msPopulate( )
{
    var resp = req.responseText;

    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(resp);

    nodes=xmlDoc.documentElement.childNodes;

    mailIDVal = xmlDoc.getElementsByTagName('mailID');

    fromUserVal = xmlDoc.getElementsByTagName('fromUser');

    subjectVal = xmlDoc.getElementsByTagName('subject');

    display();
}

function display()
{
    var fromOrTo = "From";
    if(globalIsInbox == 0)
    {
        fromOrTo = "To";
    }

    var display = "<table border='1'><tr><td></td><td class='tdFromUser'><b>" + fromOrTo + "</b></td>\n\
                <td class='tdSubject'><b>Subject</b></td></tr>";
    for(i = 0; i < fromUserVal.length; i++)
    {
        display += "<tr><td><input type=checkbox name=chkid value='" +
            mailIDVal[i].childNodes[0].nodeValue + "'/></td>";
        display += "<td class='tdFromUser'><a class='two' href='inboxMailDetails.jsp?mailID=" +
            mailIDVal[i].childNodes[0].nodeValue + "'>" +
            fromUserVal[i].childNodes[0].nodeValue + "</a></td>";
        display += "<td class='tdSubject'><a class='two' href='inboxMailDetails.jsp?mailID=" +
            mailIDVal[i].childNodes[0].nodeValue + "'>" +
            subjectVal[i].childNodes[0].nodeValue + "</a></td></tr>";
    }
    display += "</table>";

    document.getElementById("list").innerHTML = display;
}

function callback()
{
    if (req.readyState==4)
    {
        if (req.status == 200)
        {
            if (window.ActiveXObject)
            {
                msPopulate();
            }
            else
            {
                nonMSPopulate();
            }
        }
    }
}



